/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client.section;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

import java.util.ArrayList;
import java.util.List;

public class SectionList extends Composite {
  private int currentSelectionIndex = -1;

  private List sections = new ArrayList();

  private VerticalPanel verticalPanel = new VerticalPanel();

  public SectionList() {
    verticalPanel.setWidth("100%");
    initWidget(verticalPanel);
    verticalPanel.setStyleName("section-list");
  }

  public void addSection(Section section) {
    Hyperlink link = new Hyperlink(section.getName(), section.getName());
    link.setStyleName("section-label");
    verticalPanel.add(link);
    sections.add(section);
  }

  public Section get(String sectionName) {
    for (int i = 0; i < sections.size(); i++) {
      if (((Section) sections.get(i)).getName().equals(sectionName)) {
        return (Section) sections.get(i);
      }
    }
    return null;
  }

  public Section getCurrentSection() {
    if (currentSelectionIndex == -1) {
      return null;
    }

    return (Section) sections.get(currentSelectionIndex);
  }

  public void removeSection(Section section) {
    for (int i = 0; i < verticalPanel.getWidgetCount(); i++) {
      Hyperlink w = (Hyperlink) verticalPanel.getWidget(i);
      if (w.getTargetHistoryToken().equals(section.getName())) {
        verticalPanel.remove(w);
        break;
      }
    }
    sections.remove(section);
  }

  public void setSelection(Section section) {

    if (currentSelectionIndex != -1) {
      verticalPanel.getWidget(currentSelectionIndex).setStyleName(
          "section-label");
    }

    for (int i = 0; i < sections.size(); i++) {
      if (sections.get(i) == section) {
        currentSelectionIndex = i;
        verticalPanel.getWidget(currentSelectionIndex).setStyleName(
            "section-label-selected");
      }
    }
  }
}
